package cn.service.orderService.OrderServiceImpl;

import cn.service.orderService.ReturnTypeService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.Result;
import entity.orderEntity.ReturnType;
import mapper.orderMapper.ReturnTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 朱颢阳
 * @description
 * @time 2024/8/27 11:39
 */
@Service
public class ReturnTypeServiceImpl implements ReturnTypeService {
    @Resource
    ReturnTypeMapper mapper;

    @Override
    public Result getAllTypes() {
        List<ReturnType> returnTypes = mapper.selectAllType();
        JSONObject jsonObject=new JSONObject();
        for (int i = 0; i < returnTypes.size(); i++) {
            jsonObject.put(String.valueOf(returnTypes.get(i).getReturn_type_id()),returnTypes.get(i).getName());
        }
        return Result.success(200,jsonObject);
    }

    @Override
    public Result getTypes(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<ReturnType> page= (Page<ReturnType>) mapper.selectAll();
        System.out.println(page.getTotal());
        return Result.success((int)page.getTotal(),page.getResult());
    }

    @Override
    public Result updateavailable(Integer id, Integer available) {
        if (mapper.updateByPrimaryKeySelective(new ReturnType(id,available))>0){
            return Result.success("可用状态更新成功");
        }else{
            return Result.failed(500,"可用状态更新失败！");
        }
    }
    @Override
    public Result updataType(ReturnType returnType) {
        if (mapper.updateByPrimaryKeySelective(returnType)>0){
            return Result.success("更新成功");
        }else{
            return Result.failed(500,"更新失败！");
        }
    }

    @Override
    public Result insertType(ReturnType returnType) {
        returnType.setAdd_time(new Date());
        if (mapper.insertSelective(returnType)>0){
            return Result.success("添加成功");
        }else{
            return Result.failed(500,"添加失败！");
        }
    }
    @Override
    public Result deleteType(Integer id) {
        if (mapper.deleteByPrimaryKey(id)>0){
            return Result.success("删除成功");
        }else{
            return Result.failed(500,"删除失败！");
        }
    }
}
